(********************************************************************)
(*	file: 		ADSPRemove.p										*)
(*	version:	.5alpha 											*)
(* 																	*)
(* Shuts down the connection listener, deallocates memory used by	*)
(* the session.	Walks the connection list, tears them down and		*)
(* deallocates their memory.										*)
(*																	*)
(* Clears out the global container in which the handle				*)
(* is stored.														*)
(*																	*)
(* ----------------------------------------------------------------	*)
(* By:		Donald Koscheka											*)
(* Date:	3-Feb-88												*)
(* 			©	Copyright 1988, Apple Computer, Inc.				*)
(*			All Rights Reserved										*)
(*																	*)
(* ----------------------------------------------------------------	*)
(*						Modification History						*)
(* ----------------------------------------------------------------	*)
(*  Date	| By	|	 				Description					*)
(* ----------------------------------------------------------------	*)
(* 3-Feb-88	| DK	| file created									*)
(* 17-Mar-88| DK	| ALPHA RELEASE									*)
(* ----------------------------------------------------------------	*)
(********************************************************************)

(*******************************************************************
							Build Sequence

pascal -o "{hpo}"adspRemove.p.o "{adsp}"adspRemove.p
link -m ENTRYPOINT  -rt XCMD=1301 -sn Main=ADSPRemove¶
	"{hpo}"adspRemove.p.o¶
	"{libraries}"Interface.o ¶
	"{plibraries}"paslib.o¶
	-o "{hp}"HyperADSPTest
		 
*******************************************************************)


{$R-}
{$S ADSPRemove}
UNIT Koscheka;

(*******************************************************************)
INTERFACE 
(*******************************************************************)


Uses MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf, HyperXCmd, AppleTalk, ADSP, adspxcmd;


Procedure EntryPoint( paramPtr : XCmdPtr );



(*******************************************************************)
IMPLEMENTATION
(*******************************************************************)


PROCEDURE ADSPRemove( paramPtr: XCmdPtr ); FORWARD;


Procedure EntryPoint( paramPtr : XCmdPtr );
	Begin
		ADSPRemove( paramPtr );
	End;
	
	
Procedure ADSPRemove( paramPtr : XCmdPtr );
VAR
	adsp		: ADSPPtr;
	error, err	: OSErr;
	tpb			: DSPParamBlock;
	cb, nb		: CBPtr;
	mppPB		: mppParamBlock;
	
{$I XCmdGlue.inc }
{$I XCMDADSP.inc }	

BEGIN
	adsp := ADSPPtr(RetrieveHandle( GLOBALDSPDATA ));
	error := noErr;
	
	IF adsp <> NIL  THEN
		IF adsp^.checkPoint = RECEIVING THEN
			adsp^.checkPoint := CLOSE_NOW
		ELSE
			BEGIN
				cb := adsp^.ends;
				(*** 1 *** Tear down any extant connections 	***)
				WHILE cb <> NIL DO
					BEGIN
						nb := cb^.next;
						err := DSPHangUp( cb );
						DSPDisconnect( cb );
						IF error <> noErr THEN error := err;
						cb := nb;
					END;
					
				(*** 2 *** Tear down the connection listener	***)
				IF adsp^.pb.ioResult > 0 THEN
					WITH tpb DO
						BEGIN
							ioCRefNum	:= adsp^.dspRefNum;
							csCode 		:= dspCLRemove;
							ccbRefnum 	:= adsp^.ccbref;
							abort 		:= 1;
				
							err := PBControl( @tpb, SYNC );
							IF err <> noErr THEN error := err;
						END;
					
				(*** 2b **	Restore the old self send state		***)
				mppPB.newSelfFlag := adsp^.oldSelf;
				err := PSetSelfSend( @mppPB, SYNC );
				
				{*** Since PSetSelfSend is not implemented on 	***}
				{*** the Mac+ (???), ignore the result which	***}
				{*** most likely will be "driver can't respond	***}
				{*** to this control call".						***}
				
				
				(*** 3 *** Deallocate all ADSP memory			***)
				DisposPtr( Ptr( adsp ) );
				
				(*** 4 *** Clear out the containers				***)
				SaveHandle( GLOBALDSPDATA, 0 );
			END;
		
	paramPtr^.returnValue := ErrorReturn( error );
END;

end.
